package solution._0010._0010;

/**
 * Created by chen_snow
 * Date: 2021/5/6 22:09
 * Description: 正则表达式匹配
 */
public class Solution {
    public boolean isMatch(String s, String p) {
        int m = s.length();
        int n = p.length();

        boolean[][] data = new boolean[m + 1][n + 1];
        data[0][0] = true;

        for (int i = 0; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (p.charAt(j - 1) == '*') {
                    data[i][j] = data[i][j - 2];
                    if (match(s, p, i, j - 1)) {
                        data[i][j] = data[i][j] | data[i - 1][j];
                    }
                } else {
                    if (match(s, p, i, j)) {
                        data[i][j] = data[i - 1][j - 1];
                    }
                }
            }
        }
        return data[m][n];
    }

    public boolean match(String s, String p, int i, int j) {
        if (i == 0) {
            return false;
        }

        if (p.charAt(j - 1) == '.') {
            return true;
        }

        return s.charAt(i - 1) == p.charAt(j - 1);

    }
}
